﻿package mortal.game.scene3D.layer3D
{
    import Message.Public.*;
    import com.gengine.core.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.scene3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.systemSetting.*;

    public class PlayerLayer extends SLayer3D
    {
        private var _globalTimer:FrameTimer;
        private var _globalSecTimer:SecTimer;
        private var _disposeCountTime:Number = 0;
        private var _disposeMapTime:Number = 0;
        private var _disposeGImageBitmapTime:Number = 0;
        private var _specailEntityList:Array;
        private var _userDisArray:Array;
        private static var MAX_DISPOSETIME:Number = 60000;
        private static var MAX_DISPOSEBITMAPTIME:Number = 30000;
        private static var MAX_DISPOSGIMAGEBITMAP:Number = 1.2e+006;

        public function PlayerLayer(param1:String)
        {
            this._specailEntityList = [];
            this._userDisArray = [];
            super(param1);
            ThingUtil.iniThing(this);
            this._globalTimer = new FrameTimer();
            this._globalTimer.addListener(TimerType.ENTERFRAME, this.onTimerFrameHandler);
            this._globalTimer.start();
            this._globalSecTimer = new SecTimer(5);
            this._globalSecTimer.addListener(TimerType.ENTERFRAME, this.onSecTimerHandler);
            this._globalSecTimer.start();
            return;
        }// end function

        private function onSecTimerHandler(param1:SecTimer) : void
        {
            this.entityTalk();
            return;
        }// end function

        private function onTimerFrameHandler(param1:FrameTimer) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Boolean = false;
            if (Game.scene.isInitialize)
            {
                _loc_2 = param1.currentCount % 5;
                this.updateEntity();
                _loc_3 = this.createEntitys();
                if (_loc_2 == 4 || _loc_3)
                {
                    this.sortEntitys();
                }
                this.framesDataDispose(param1);
                this.mapBitmapDispose(param1);
                this.gimageBitmapDispose(param1);
            }
            return;
        }// end function

        private function framesDataDispose(param1:FrameTimer) : void
        {
            this._disposeCountTime = this._disposeCountTime + param1.interval;
            if (this._disposeCountTime >= MAX_DISPOSETIME)
            {
                this._disposeCountTime = 0;
                FramesData.dispose();
            }
            return;
        }// end function

        private function mapBitmapDispose(param1:FrameTimer) : void
        {
            this._disposeMapTime = this._disposeMapTime + param1.interval;
            if (this._disposeMapTime >= MAX_DISPOSEBITMAPTIME)
            {
                this._disposeMapTime = 0;
                Game.scene.topMapLayer.disposeBitmap();
            }
            return;
        }// end function

        private function gimageBitmapDispose(param1:FrameTimer) : void
        {
            this._disposeGImageBitmapTime = this._disposeGImageBitmapTime + param1.interval;
            if (this._disposeGImageBitmapTime > MAX_DISPOSGIMAGEBITMAP)
            {
                this._disposeGImageBitmapTime = 0;
                GImageBitmap.diposeInfo();
                GLoadingButton.diposeBitmap();
                RoleResourceUtil.checkRoleSkill();
            }
            return;
        }// end function

        public function sortEntitys() : void
        {
            if (ThingUtil.isHeadContainerChange)
            {
                ThingUtil.entityUtil.updateHeadContainer();
                ThingUtil.isHeadContainerChange = false;
            }
            if (ThingUtil.isEntitySort)
            {
                if (ThingUtil.isNameChange)
                {
                    Cache.instance.entity.updateAllName();
                    ThingUtil.isNameChange = false;
                }
                if (ThingUtil.isMoveChange)
                {
                    ThingUtil.isMoveChange = false;
                    ThingUtil.npcUtil.updateEntity();
                    ThingUtil.passUtil.updateEntity();
                    ThingUtil.jumpUtil.updateEntity();
                    ThingUtil.effectUtil.updateEntity();
                    ThingUtil.effectSound.updateEntity();
                    ThingUtil.dropItemUtil.updateEntity();
                    ThingUtil.pixsMaskUtil.updateEntity();
                    if (ThingUtil.selectEntity)
                    {
                        if (!SceneRange.isInEffectRange(ThingUtil.selectEntity.x2d, ThingUtil.selectEntity.y2d))
                        {
                            ThingUtil.selectEntity = null;
                        }
                    }
                    ThingUtil.statueUtil.updateEntity();
                }
                this.addAllEntity();
                if (Global.isDebugModle)
                {
                    FPS.instance.entityNum = ThingUtil.sceneEntityList.length;
                }
                ThingUtil.isEntitySort = false;
            }
            return;
        }// end function

        private function addAllEntity() : void
        {
            var _loc_5:IEntity = null;
            var _loc_6:UserPlayer = null;
            var _loc_7:MonsterPlayer = null;
            var _loc_8:EntityInfo = null;
            var _loc_9:Boolean = false;
            var _loc_10:Boolean = false;
            var _loc_11:BossInfo = null;
            var _loc_12:Boolean = false;
            var _loc_13:Boolean = false;
            var _loc_14:Boolean = false;
            var _loc_1:* = Cache.instance.role.entityInfo.entityId;
            this._specailEntityList.length = 0;
            this._userDisArray.length = 0;
            var _loc_2:* = SystemSetting.instance.hideSetter.bValue || MapFileUtil.mapID == GameConst.FirstMapID && FrameUtil.isDriverSoftRender;
            var _loc_3:* = SystemSetting.instance.hideSetterType.value;
            var _loc_4:Boolean = false;
            for each (_loc_5 in ThingUtil.entityUtil.entitysMap.allEntitys)
            {
                
                _loc_8 = _loc_5.entityInfo;
                if (!_loc_8)
                {
                    continue;
                }
                _loc_9 = _loc_8.isDisappear && (!Cache.instance.group.entityIsInGroup(_loc_8.entityInfo.entityId) || !EntityRelationUtil.isFriend(_loc_8.entityInfo));
                _loc_10 = false;
                if (_loc_5 is MonsterPlayer)
                {
                    _loc_11 = (_loc_5 as MonsterPlayer).bossInfo;
                    if (_loc_11 && ThingUtil.entityUtil.isBossHide(_loc_11.code))
                    {
                        _loc_10 = true;
                    }
                }
                if (SceneRange.isInSceneByXY(_loc_5.x2d, _loc_5.y2d) && !_loc_9 && !_loc_10)
                {
                    _loc_12 = false;
                    _loc_13 = false;
                    if (_loc_5 is PetPlayer)
                    {
                        _loc_12 = _loc_2 && (_loc_3 == 1 || _loc_3 == 2 && !_loc_8.isEnemy || _loc_3 == 3 && _loc_8.isEnemy);
                        _loc_13 = _loc_8.isInRoleGroup || _loc_8.isAttackRole || _loc_5.selected;
                        if (!_loc_12 || _loc_13 || _loc_8.isSelf)
                        {
                            _loc_5.addToStage(this);
                        }
                        else
                        {
                            _loc_5.removeFromStage(this, true);
                        }
                    }
                    else if (_loc_5 is UserPlayer)
                    {
                        if (_loc_5 is RolePlayer)
                        {
                            continue;
                        }
                        _loc_12 = _loc_2 && (_loc_3 == 1 || _loc_3 == 2 && !_loc_8.isEnemy || _loc_3 == 3 && _loc_8.isEnemy);
                        _loc_13 = _loc_8.isInRoleGroup || _loc_8.isAttackRole || _loc_5.selected || EntityUtil.equal(_loc_8.zazenEntityId, _loc_1) || EntityRelationUtil.isOwnerSelf(_loc_8.entityInfo);
                        _loc_14 = _loc_8.entityInfo.ownerEntityId && _loc_8.entityInfo.ownerEntityId.id > 0;
                        _loc_6 = _loc_5 as UserPlayer;
                        if (!_loc_12 || _loc_13 || _loc_5 is RolePlayer)
                        {
                            _loc_6.addToStage(this);
                            if (_loc_5 is RolePlayer)
                            {
                                continue;
                            }
                            if (_loc_13)
                            {
                                this.addUserToSpecialArray(_loc_6);
                            }
                            else if (!_loc_14)
                            {
                                this.addUserToAry(_loc_6);
                            }
                        }
                        else
                        {
                            _loc_6.removeFromStage(this, false);
                        }
                    }
                    else if (_loc_5 is MonsterPlayer)
                    {
                        _loc_7 = _loc_5 as MonsterPlayer;
                        _loc_7.addToStage(this);
                    }
                    else
                    {
                        _loc_5.addToStage(this);
                    }
                    continue;
                }
                if (_loc_5 is RolePlayer == false)
                {
                    _loc_5.removeFromStage(this, true);
                }
            }
            this.userOverStep();
            return;
        }// end function

        private function userOverStep() : void
        {
            var _loc_5:UserPlayer = null;
            var _loc_6:int = 0;
            if (!SystemSetting.instance.playerOnScreen.bValue)
            {
                return;
            }
            var _loc_1:* = this._userDisArray.length;
            var _loc_2:* = _loc_1;
            var _loc_3:Boolean = false;
            var _loc_4:* = SystemSetting.instance.playerOnScreenNum.value - this._specailEntityList.length;
            if (_loc_4 < 0)
            {
                _loc_4 = 0;
            }
            if (_loc_1 > _loc_4)
            {
                this._userDisArray.sort(this.onDistanceSort);
                _loc_1 = this._userDisArray.length;
                _loc_6 = _loc_1 - 1;
                while (_loc_6 >= _loc_4)
                {
                    
                    this.removeEntityAll(this._userDisArray[_loc_6]);
                    _loc_6 = _loc_6 - 1;
                }
            }
            return;
        }// end function

        private function removeEntityAll(param1:UserPlayer) : void
        {
            param1.removeFromStage(this, true);
            var _loc_2:* = param1.getSelfPet();
            if (_loc_2)
            {
                _loc_2.removeFromStage(this, true);
            }
            return;
        }// end function

        private function onDistanceSort(param1:UserPlayer, param2:UserPlayer) : int
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (param1.entityInfo.isInFriend)
            {
                _loc_3 = _loc_3 + 4;
            }
            if (param1.entityInfo.isInRoleGuild)
            {
                _loc_3 = _loc_3 + 2;
            }
            if (param2.entityInfo.isInFriend)
            {
                _loc_4 = _loc_4 + 4;
            }
            if (param2.entityInfo.isInRoleGuild)
            {
                _loc_4 = _loc_4 + 2;
            }
            if (_loc_4 > _loc_3)
            {
                return -1;
            }
            if (_loc_3 > _loc_4)
            {
                return 1;
            }
            if (param1.toRoleDistance < param2.toRoleDistance)
            {
                return -1;
            }
            if (param1.toRoleDistance > param2.toRoleDistance)
            {
                return 1;
            }
            return 0;
        }// end function

        private function addUserToSpecialArray(param1:UserPlayer) : void
        {
            this._specailEntityList.push(param1);
            return;
        }// end function

        private function addUserToAry(param1:UserPlayer) : void
        {
            this._userDisArray.push(param1);
            return;
        }// end function

        private function createEntitys() : Boolean
        {
            var _loc_1:Boolean = false;
            var _loc_2:* = Cache.instance.entity.getFromOffAry();
            while (_loc_2 != null)
            {
                
                _loc_1 = true;
                ThingUtil.entityUtil.createEntity(_loc_2);
                ThingUtil.shareCollectEntityUtil.createEntity(_loc_2);
                ThingUtil.guildCollectTreeUtil.createEntity(_loc_2);
                Cache.instance.entity.removeFromOffAry(_loc_2.entityInfo.entityId);
                _loc_2 = Cache.instance.entity.getFromOffAry();
            }
            return _loc_1;
        }// end function

        private function updateEntity() : void
        {
            var _loc_1:SpritePlayer = null;
            for each (_loc_1 in ThingUtil.entityUtil.entitysMap.allEntitys)
            {
                
                _loc_1.updateDisplay();
            }
            FrameManager.isSecUpdate = false;
            return;
        }// end function

        private function entityTalk() : void
        {
            var _loc_1:SpritePlayer = null;
            var _loc_2:MonsterPlayer = null;
            for each (_loc_1 in ThingUtil.entityUtil.entitysMap.allEntitys)
            {
                
                if (_loc_1 is MonsterPlayer)
                {
                    _loc_2 = _loc_1 as MonsterPlayer;
                    _loc_2.judgeLifeTalk();
                    _loc_2.judgeNormalTalk();
                }
            }
            return;
        }// end function

    }
}
